<!--

/*
** Copyright (c) 2012 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/

-->

<!DOCTYPE html>
<html>
  <head>
<meta charset="utf-8">
    <title>Rendering Test</title>
    <link rel="stylesheet" href="../../resources/js-test-style.css"/>
    <script src="../../resources/js-test-pre.js"></script>
    <script src="../resources/webgl-test.js"> </script>
</head>
<body>
<canvas id="example" width="512" height="512" style="width: 512px; height: 512px;">
There is supposed to be an example drawing here, but it's not important.
</canvas>
<div id="description"></div>
<div id="console"></div>
    <script id="vshader" type="x-shader/x-vertex">
        attribute vec4 vPosition;
        void main()
        {
            gl_Position = vPosition;
        }
    </script>

    <script id="fshader" type="x-shader/x-fragment">
        void main()
        {
            gl_FragColor = vec4(1.0,0.0,0.0,1.0);
        }
    </script>

    <script>
        function fail(x,y, buf, shouldBe)
        {
            var i = (y*50+x) * 4;
            var reason = "pixel at ("+x+","+y+") is ("+buf[i]+","+buf[i+1]+","+buf[i+2]+","+buf[i+3]+"), should be "+shouldBe;
            testFailed(reason);
        }

        function pass()
        {
            testPassed("drawing is correct");
        }

        function init()
        {
            if (window.initNonKhronosFramework) {
                window.initNonKhronosFramework(false);
            }

            description(document.title);

            canvas = document.getElementById("example");
            gl = initWebGL("example");
            program = setupProgram(gl, "vshader", "fshader", [ "vPosition"]);

            // Scale the canvas till the backbuffer can't keep up
            while(gl.drawingBufferWidth == canvas.width && gl.drawingBufferHeight == canvas.height) {
                canvas.width *= 2
                canvas.height *= 2
            }

            canvas.width = gl.drawingBufferWidth;
            canvas.height = gl.drawingBufferHeight;
            gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);

            var vertexObject = gl.createBuffer();
            gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
            gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0.5,0, -0.5,-0.5,0, 0.5,-0.5,0 ]), gl.STATIC_DRAW);
            gl.enableVertexAttribArray(0);
            gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);

            var buf = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4);

            function checkPixel(buf, x, y, expected) {
                var i = (y * gl.drawingBufferWidth * 4) + (x * 4);

                if (buf[i] != expected[0] ||
                    buf[i+1] != expected[1] ||
                    buf[i+2] != expected[2]||
                    buf[i+3] != expected[3]) {
                    var reason = "pixel at ("+x+","+y+") is ("+buf[i]+","+buf[i+1]+","+buf[i+2]+","+buf[i+3]+"), should be ("+expected[0]+","+expected[1]+","+expected[2]+","+expected[3]+")";
                    testFailed(reason);
                }
            }

            function drawFrame() {
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

                gl.useProgram(program);
                gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
                gl.enableVertexAttribArray(0);
                gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
                gl.drawArrays(gl.TRIANGLES, 0, 3);

                gl.readPixels(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, buf);

                checkPixel(buf, 0, 0, [0,0,0,255]);
                checkPixel(buf, gl.drawingBufferWidth*0.5, gl.drawingBufferHeight*0.5, [255,0,0,255]);

                requestAnimationFrame(drawFrame, canvas);
            }

            drawFrame();

            pass();
       }

       init();
       successfullyParsed = true;
    </script>
<script src="../../resources/js-test-post.js"></script>

</body>
</html>
